﻿namespace SharpGym.Silverlight.Views
{
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Navigation;
    using SharpService;

    public partial class Alunos
    {
        public Alunos()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Busy.IsBusy = true;
            App.Client.ReadAlunosAsync();
            App.Client.ReadAlunosCompleted += ClientOnReadAlunosCompleted;

            EditAlunoView.Busy = Busy;
            NewAlunoView.Busy = Busy;
        }

        private void ClientOnReadAlunosCompleted(object sender,
                                                 ReadAlunosCompletedEventArgs readAlunosCompletedEventArgs)
        {
            App.Client.ReadAlunosCompleted -= ClientOnReadAlunosCompleted;
            //Load your data here and assign the result to the CollectionViewSource.
            var myCollectionViewSource = (CollectionViewSource) Resources["alunoDtoViewSource"];
            myCollectionViewSource.Source = readAlunosCompletedEventArgs.Result.OrderBy(a => a.Nome);
            AlunosList.SelectedIndex = -1;
            ViewAlunoView.Advertising.Visibility = Visibility.Visible;
            ViewAlunoView.ContentView.Visibility = Visibility.Collapsed;
            Busy.IsBusy = false;
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            NewAlunoView.Visibility = EditAlunoView.Visibility = ViewAlunoView.Visibility = Visibility.Collapsed;
            NewAlunoView.Visibility = Visibility.Visible;
            NewAlunoView.DataContext = new AlunoDto();
            RemoveButton.IsEnabled = false;
        }

        private void AlunosList_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NewAlunoView.Visibility = EditAlunoView.Visibility = ViewAlunoView.Visibility = Visibility.Collapsed;
            ViewAlunoView.Visibility = Visibility.Visible;
            ViewAlunoView.ContentView.Visibility = Visibility.Visible;
            ViewAlunoView.Advertising.Visibility = Visibility.Collapsed;
            RemoveButton.IsEnabled = true;
            EditButton.IsEnabled = true;
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            NewAlunoView.Visibility = EditAlunoView.Visibility = ViewAlunoView.Visibility = Visibility.Collapsed;
            EditAlunoView.Visibility = Visibility.Visible;
            RemoveButton.IsEnabled = true;
        }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Deseja realmente remover esse Aluno?", "Remoção de Aluno", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                return;

            var aluno = (AlunoDto) ViewAlunoView.DataContext;
            Busy.IsBusy = true;
            App.Client.DeleteAlunoAsync(aluno);
            App.Client.DeleteAlunoCompleted += ClientOnDeleteAlunoCompleted;
        }

        private void ClientOnDeleteAlunoCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs)
        {
            App.Client.DeleteAlunoCompleted -= ClientOnDeleteAlunoCompleted;
            Busy.IsBusy = false;
            MessageBox.Show("Aluno Removido");
            Busy.DataContext = new Random().Next();
        }

        private void Busy_OnDataContextChanged(object sender,
                                               DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            Busy.IsBusy = true;
            App.Client.ReadAlunosAsync();
            App.Client.ReadAlunosCompleted += ClientOnReadAlunosCompleted;
        }
    }
}